home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Magazine / C_Tutorial / Part-4 / screen3.c < prev    next >
C/C++ Source or Header  |  1997-08-31  |  10KB  |  361 lines

  1. /* First, include our own supporting header files, i.e., clip.h */
  2. #include "clip.h"
  3.  
  4. #include<exec/libraries.h>
  5. #include<intuition/intuition.h>
  6. #include<utility/tagitem.h>
  7. #include<graphics/text.h>
  8. #include<graphics/rastport.h>
  9. #include<intuition/screens.h>
  10. #include<libraries/gadtools.h>
  11.  
  12. #include<string.h>
  13. #include<stdio.h>
  14.  
  15. #include<clib/exec_protos.h>
  16. #include<clib/graphics_protos.h>
  17. #include<clib/intuition_protos.h>
  18. #include<clib/gadtools_protos.h>
  19.  
  20. /* The library base global variables */
  21. /* (The different style of opening libraries requires these to be initialised to NULL) */
  22. struct Library* GfxBase = NULL;
  23. struct Library* IntuitionBase = NULL;
  24. struct Library* LayersBase = NULL;
  25. struct Library* GadToolsBase = NULL;
  26.  
  27. /* The global handle on the palette gadget */
  28. struct Gadget* palgad = NULL;
  29.  
  30. /* Need to give prototypes for our functions */
  31. void handleIDCMP(struct Window*);
  32. void setupWindow();
  33. void createWindow(struct Gadget*, struct Screen*, struct Menu*);
  34. int openLibs();
  35. void closeLibs();
  36. struct Menu* createMenuStrip(APTR);
  37.  
  38. /* Some constants for the position and size of our gadget */
  39. #define MYBUT_LEFT        (10)
  40. #define MYBUT_TOP            (5)
  41. #define MYBUT_WIDTH        (80)
  42. #define MYBUT_HEIGHT    (12)
  43. #define MYBUT_TEXT        "Next Pen"
  44. #define MYBUT_ID            (0)
  45.  
  46. #define MYPAL_LEFT        (170)
  47. #define MYPAL_TOP            (2)
  48. #define MYPAL_WIDTH        (109)
  49. #define MYPAL_HEIGHT    (19)
  50. #define MYPAL_TEXT        "Colour:"
  51. #define MYPAL_ID            (1)
  52. #define MYPAL_DEPTH        (4)
  53.  
  54. /* The top gap required around the gadgets */
  55. #define MYTOPGAP      (30)
  56.  
  57. /* The initial pen colour */
  58. #define MYINITPEN            (1)
  59.  
  60. /* The start of the program */
  61. void main()
  62. {
  63.     /* Use a different style of opening libraries... */
  64.     if(openLibs())
  65.     {
  66.         /* Now do the real work */
  67.         setupWindow();
  68.     }
  69.     /* Matched call to close libraries */
  70.     closeLibs();
  71. }
  72.  
  73. /* Try to open all the libraries -- return TRUE on success */
  74. int openLibs()
  75. {
  76.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  77.     {
  78.         printf("Error: could not open graphics.library\n");
  79.         return FALSE;
  80.     }
  81.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  82.     {
  83.         printf("Error: could not open intuition.library\n");
  84.         return FALSE;
  85.     }
  86.     if((LayersBase = OpenLibrary("layers.library",37)) == NULL)
  87.     {
  88.         printf("Error: could not open layers.library\n");
  89.         return FALSE;
  90.     }
  91.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  92.     {
  93.         printf("Error: could not open gadtools.library\n");
  94.         return FALSE;
  95.     }
  96.   return TRUE;
  97. }
  98.  
  99. /* Close any open library */
  100. void closeLibs()
  101. {
  102.     if(GadToolsBase)
  103.         CloseLibrary(GadToolsBase);
  104.     if(LayersBase)
  105.         CloseLibrary(LayersBase);
  106.     if(IntuitionBase)
  107.         CloseLibrary(IntuitionBase);
  108.     if(GfxBase)
  109.         CloseLibrary(GfxBase);
  110. }
  111.  
  112. /* Setup the window -- do the GadTools stuff */
  113. void setupWindow()
  114. {
  115.     struct Screen* scr;
  116.     UWORD pens[] = { ~0 };
  117.     /* Try to open a new screen with 16 colours (four bitplanes deep) */
  118.     if(scr = OpenScreenTags(NULL,
  119.                                                     SA_Depth,    4,
  120.                                                     /* Enable 3D look by specifying SA_Pens */
  121.                                                     SA_Pens,    pens,
  122.                                                     TAG_DONE))
  123.     {
  124.         APTR vinfo;
  125.         /* Get the visual info so GadTools can render the gadgets nicely */
  126.         if(vinfo = GetVisualInfo(scr, TAG_DONE))
  127.         {
  128.             /* We can initialise glist in its declaration */
  129.             struct Gadget* glist = NULL;
  130.             struct Gadget* gad;
  131.             int offtop, offleft;
  132.             struct NewGadget newgad;
  133.             /* Initialised structure declaration: describes 8pt Topaz font */
  134.             struct TextAttr topazFont = { "topaz.font", 8, 0, 0, };
  135.             /* Start a GadTools gadget list */
  136.             gad = CreateContext(&glist);
  137.             /* The offsets of our window borders */
  138.             offleft = scr->WBorLeft;
  139.             offtop = scr->WBorTop + (scr->Font->ta_YSize + 1);
  140.  
  141.             /* Setup our first gadget */
  142.             newgad.ng_TextAttr         = &topazFont;
  143.             newgad.ng_VisualInfo     = vinfo;
  144.             newgad.ng_LeftEdge         = MYBUT_LEFT + offleft;
  145.             newgad.ng_TopEdge         = MYBUT_TOP + offtop;
  146.             newgad.ng_Width             = MYBUT_WIDTH;
  147.             newgad.ng_Height             = MYBUT_HEIGHT;
  148.             newgad.ng_GadgetText    = MYBUT_TEXT;
  149.             newgad.ng_GadgetID        = MYBUT_ID;
  150.             newgad.ng_Flags                = 0;
  151.             /* Now create it and add it to our list */
  152.             gad = CreateGadget(BUTTON_KIND, gad, &newgad, TAG_END);
  153.  
  154.             /* Setup our second gadget */
  155.             /* (We can reuse newgad, and just change the different bits) */
  156.             newgad.ng_LeftEdge         = MYPAL_LEFT + offleft;
  157.             newgad.ng_TopEdge         = MYPAL_TOP + offtop;
  158.             newgad.ng_Width             = MYPAL_WIDTH;
  159.             newgad.ng_Height             = MYPAL_HEIGHT;
  160.             newgad.ng_GadgetText    = MYPAL_TEXT;
  161.             newgad.ng_GadgetID        = MYPAL_ID;
  162.             newgad.ng_Flags                = 0;
  163.             /* Now create it and add it to our list */
  164.             if(gad = CreateGadget(PALETTE_KIND, gad, &newgad,
  165.                                                         /* Initially selected pen */
  166.                                                         GTPA_Color, MYINITPEN,
  167.                                                         /* Depth: 2 to the power MYPAL_DEPTH colours */
  168.                                                         GTPA_Depth, MYPAL_DEPTH,
  169.                                                         /* Gadget will indicate selection */
  170.                                                         GTPA_IndicatorWidth, 16,
  171.                                                         TAG_DONE))
  172.             {
  173.                 struct Menu* menustrip;
  174.                 /* Remember gadget pointer so we can affect it in message handler */
  175.                 palgad = gad;
  176.                 if(menustrip = createMenuStrip(vinfo))
  177.                     /* If succeeded then all gadgets and menus created */
  178.                     createWindow(glist, scr, menustrip);
  179.                 FreeMenus(menustrip);
  180.             }
  181.             else
  182.                 printf("Error: could not create gadget(s)\n");
  183.             /* Free all the gadgets that were created */
  184.             FreeGadgets(glist);
  185.             FreeVisualInfo(vinfo);
  186.         }
  187.         else
  188.             printf("Error: could not get visual info\n");
  189.         CloseScreen(scr);
  190.     }
  191.     else
  192.         printf("Error: could not create screen\n");
  193. }
  194.  
  195. /* Create the menu strip, using GadTools menu functions */
  196. struct Menu* createMenuStrip(APTR vinfo)
  197. {
  198.     /* The description of our menus */
  199.     struct NewMenu mymenu[] =
  200.     {
  201.         { NM_TITLE, "Project",    0,        0, 0, 0,},
  202.         {  NM_ITEM,        "Quit",        "Q",    0, 0, 0,},
  203.         {   NM_END, NULL,                0,        0, 0, 0,},
  204.     };
  205.     struct Menu* menustrip;
  206.     if (menustrip = CreateMenus(mymenu, TAG_END))
  207.     {
  208.         if (LayoutMenus(menustrip, vinfo, TAG_END))
  209.             /* Succeeded, so return menu strip */
  210.             return menustrip;
  211.         else
  212.         {
  213.             /* Failed, so must deallocate before returning */
  214.             FreeMenus(menustrip);
  215.             printf("Error: could not layout menus\n");
  216.         }
  217.     }
  218.     else
  219.         printf("Error: could not create menu strip\n");
  220.     /* Failed, so return NULL */
  221.     return NULL;
  222. }
  223.  
  224. /* Actually open the window, in the normal way */
  225. void createWindow(struct Gadget* glist, struct Screen* scr, struct Menu* menustrip)
  226. {
  227.     struct Window* win;
  228.     /* Open our window */
  229.     if(win = OpenWindowTags(NULL,
  230.                                                     WA_Left,                    0,
  231.                                                     WA_Top,                        0,
  232.                                                     /* Make the window the same size as the screen */
  233.                                                     WA_Width,                    scr->Width,
  234.                                                     WA_Height,                scr->Height,
  235.                                                     WA_Flags,                    WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_REPORTMOUSE,
  236.                                                     WA_IDCMP,                    IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE | BUTTONIDCMP | IDCMP_REFRESHWINDOW | IDCMP_MENUPICK,
  237.                                                     WA_Gadgets,                glist,
  238.                                                     WA_CustomScreen,    scr,
  239.                                                     TAG_DONE,                    0))
  240.     {
  241.         /* If window opened, set clip region */
  242.         if(setClipInternal(win))
  243.         {
  244.             /* Attach menu strip to window */
  245.             if(SetMenuStrip(win, menustrip))
  246.             {
  247.                 /* Let GadTools refresh its bits of the window */
  248.                 GT_RefreshWindow(win, NULL);
  249.                 /* Now handle messages */
  250.                 handleIDCMP(win);
  251.                 /* Remove menu strip */
  252.                 ClearMenuStrip(win);
  253.             }
  254.             else
  255.                 printf("Error: could not attach menus to window\n");
  256.             removeClip(win);    
  257.         }
  258.         else
  259.             printf("Error: could not set clip region on window\n");
  260.         CloseWindow(win);
  261.     }
  262.     else
  263.         printf("Error: could not open window\n");
  264. }
  265.  
  266. /* Our message handling code */
  267. void handleIDCMP(struct Window* win)
  268. {
  269.     char* text = "Hello World!";
  270.     int going = TRUE;
  271.     int drawing = FALSE;
  272.     UBYTE pen = MYINITPEN;
  273.     SetAPen(win->RPort, pen);
  274.     /* Set the drawing mode to draw only the foreground of text, not the background */
  275.     SetDrMd(win->RPort, JAM1);
  276.     while(going)
  277.     {
  278.         struct IntuiMessage* intuimsg;
  279.         /* Wait for messages to arrive */
  280.         WaitPort(win->UserPort);
  281.         /* Messages have arrived: loop through all of them */
  282.         while(intuimsg = GT_GetIMsg(win->UserPort))
  283.         {
  284.             /* Act on this message... */
  285.             switch(intuimsg->Class)
  286.             {
  287.             case IDCMP_MOUSEBUTTONS:
  288.                 switch(intuimsg->Code)
  289.                 {
  290.                 case SELECTDOWN:
  291.                     drawing = TRUE;
  292.                     break;
  293.                 case SELECTUP:
  294.                     drawing = FALSE;
  295.                     break;
  296.                 }
  297.                 /* break; omitted so we draw on click, too */
  298.             case IDCMP_MOUSEMOVE:
  299.                 /* Don't draw on top gap which holds gadgets */
  300.                 if(drawing && intuimsg->MouseY > win->BorderTop+MYTOPGAP)
  301.                 {
  302.                     Move(win->RPort, intuimsg->MouseX, intuimsg->MouseY);
  303.                     Text(win->RPort, text, strlen(text));
  304.                 }
  305.                 break;
  306.             case IDCMP_CLOSEWINDOW:
  307.                 going = FALSE;
  308.                 break;
  309.             case IDCMP_REFRESHWINDOW:
  310.                 /* You *MUST* remember to ask for and handle these refresh messages */
  311.                 GT_BeginRefresh(win);
  312.                 GT_EndRefresh(win, TRUE);
  313.                 break;
  314.             case IDCMP_GADGETUP:
  315.                 /* Trick: introduce new "{..}" scope so we can declare gad locally */
  316.                 {
  317.                     struct Gadget* gad = (struct Gadget*)(intuimsg->IAddress);
  318.                     switch(gad->GadgetID)
  319.                     {
  320.                     case MYBUT_ID:
  321.                         /* Our button was clicked!  Set foreground to next pen colour */
  322.                         /* (Wrap when reached the end of the palette gadget's colours) */
  323.                         pen = (pen+1) % (1<<MYPAL_DEPTH);
  324.                         SetAPen(win->RPort, pen);
  325.                         /* Update palette gadget with new pen value */
  326.                         GT_SetGadgetAttrs(palgad, win, NULL, GTPA_Color, pen, TAG_DONE);
  327.                         break;
  328.                     case MYPAL_ID:
  329.                         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  330.                         pen = intuimsg->Code;
  331.                         SetAPen(win->RPort, pen);
  332.                         break;
  333.                     }
  334.                     break;
  335.                 }
  336.             case IDCMP_MENUPICK:
  337.                 /* Again, use trick to introduce new "{..}" scope */
  338.                 {
  339.                     UWORD menuCode, menuNumber, itemNumber;
  340.                     /* Loop over all the menu selections in the menu code */
  341.                     for(menuCode = intuimsg->Code;
  342.                             going && menuCode != MENUNULL;
  343.                             menuCode = ItemAddress(win->MenuStrip, menuCode)->NextSelect)
  344.                     {
  345.                         /* Extract the menu number and menu item number from the menu code */
  346.                         menuNumber = MENUNUM(menuCode);
  347.                         itemNumber = ITEMNUM(menuCode);
  348.                         /* Now decide what to do based on what menu item was selected */
  349.                         /* Only one item: Project=>Quit */
  350.                         if(menuNumber == 0 && itemNumber == 0)
  351.                             going = FALSE;
  352.                     }
  353.                     break;
  354.                 }
  355.             }
  356.             /* Reply when finished with message */
  357.             GT_ReplyIMsg(intuimsg);
  358.         }
  359.     }
  360. }
  361.